package com.vend.main.splash.presenter
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.CountDownTimer
import android.util.Log
import com.vend.common.mvp.IPresenter
import com.vend.main.home.HomeActivity
import com.vend.main.splash.SplashView

/**
 * Created by glendal on 2022/01/25.
 * 启动页的逻辑处理
 */
class SplashPresenter(val self : Context ,val v : SplashView) : IPresenter {
    var time : CountDownTimer ?= null
    override fun start() {
        createTimmer()
    }

    /**
     * 创建一个定时器
     */
    private fun createTimmer(){
        if (time != null)
            time?.cancel()
        //倒计时的计时器的创建
        time = object : CountDownTimer(SPLASH_DELAY_TIME, 1000) {
            override fun onFinish() {
                Log.e("tag","finish")
                //倒计时完成
                self.startActivity(Intent(self,HomeActivity::class.java))
                (self as Activity).finish()
            }

            override fun onTick(millisUntilFinished: Long) {
                val t = millisUntilFinished / 1000
                Log.e("tag","time  $t")
                v.showTime("${t + 1 }S")
            }
        }
        time?.start()
    }
    override fun stop() {
        //关闭界面的时候记得取消掉倒计时
        time?.cancel()
    }
    companion object{
        //倒计时定义最长的时间
        const val SPLASH_DELAY_TIME = 3 * 1000L
    }
}